package org.linlinjava.litemall.admin.service.order_manage;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.linlinjava.litemall.admin.enums.AdminErrorCode;
import org.linlinjava.litemall.admin.vo.full_gift.AddGiftActiveVo;
import org.linlinjava.litemall.admin.vo.full_gift.GiftActiveCountUserVo;
import org.linlinjava.litemall.admin.vo.full_gift.GiftBean;
import org.linlinjava.litemall.admin.vo.full_gift.GiftListVo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.AgedmallActivityRuleMapper;
import org.linlinjava.litemall.db.dao.order_manage.AgedmallFullGiftMapper;
import org.linlinjava.litemall.db.domain.AgedmallActivityRule;
import org.linlinjava.litemall.db.vo.full_gift.ActivityListDto;
import org.linlinjava.litemall.db.vo.full_gift.GoodsUseCountDto;
import org.linlinjava.litemall.db.vo.full_gift.UserGoodsProductDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wj
 * @date 2021/8/30 14:20
 */
@Service
public class AgedmallFullGiftService {



    @Resource
    private AgedmallFullGiftMapper agedmallFullGiftMapper;

    @Resource
    private AgedmallActivityRuleMapper agedmallActivityRuleMapper;



    public Object addGiftActive(AddGiftActiveVo addGiftActiveVo){
        AgedmallActivityRule agedmallActivityRule;
        if(ObjectUtil.isNull(addGiftActiveVo.getActiveTime())){
            return ResponseUtil.fail(AdminErrorCode.ACTIVITYTIME_NOT_NULL.getCode(),
                    AdminErrorCode.ACTIVITYTIME_NOT_NULL.getMessage());
        }
        if(addGiftActiveVo.getId()!=null){
            agedmallActivityRule=agedmallActivityRuleMapper.selectByPrimaryKey(addGiftActiveVo.getId());
        }
        else{
            agedmallActivityRule=new AgedmallActivityRule();
            agedmallActivityRule.setAddTime(LocalDateTime.now());
        }
        agedmallActivityRule.setName(addGiftActiveVo.getActiveName());
        agedmallActivityRule.setStartTime(addGiftActiveVo.getActiveTime().getStartTime());
        agedmallActivityRule.setEndTime(addGiftActiveVo.getActiveTime().getEndTime());
        agedmallActivityRule.setCanFullgiftStoreType(addGiftActiveVo.getSupportType());
        agedmallActivityRule.setCanFullgiftStore(JSON.toJSONString(addGiftActiveVo.getShopIds()));
        agedmallActivityRule.setActivityGoods(addGiftActiveVo.getActiveCommodityIds());
        List<GiftBean> giftBeans=addGiftActiveVo.getGiftList();
        String[] giftIds=new String[giftBeans.size()];
        for (int i = 0; i <giftBeans.size() ; i++) {
            giftIds[i]=giftBeans.get(i).getCommodityid().toString();
        }
        agedmallActivityRule.setSendGoodsIds(StringUtils.join(giftIds,","));
        agedmallActivityRule.setSendGoods(JSON.toJSONString(giftBeans));
        agedmallActivityRule.setType(0);
        agedmallActivityRule.setFullMoney(addGiftActiveVo.getFullMoney().longValue());
        agedmallActivityRule.setCanChooseSize(addGiftActiveVo.getSelectedNumber());
        agedmallActivityRule.setStat(1);
        agedmallActivityRule.setRunningStat(0);
        if(addGiftActiveVo.getId()!=null){
            agedmallActivityRuleMapper.updateByPrimaryKey(agedmallActivityRule);
        }
        else{
            agedmallActivityRuleMapper.insertSelective(agedmallActivityRule);
        }
        return ResponseUtil.ok();
    }


    public Object giftList(GiftListVo giftListVo){
        Page page= PageHelper.startPage(giftListVo.getPage(),giftListVo.getLimit());
        List<ActivityListDto> activityListDtos = agedmallFullGiftMapper.selectActivityList(giftListVo.getState());
        Map<String, Object> map=new HashMap<>();
        map.put("total",page.getTotal());
        map.put("data",activityListDtos);
        return ResponseUtil.ok(map);
    }


    public Object giftActiveCount(Integer id){
        List<GoodsUseCountDto> goodsUseCountDtos = agedmallFullGiftMapper.selectGoodsUseCountByActivityId(id);
        return ResponseUtil.ok(goodsUseCountDtos);
    }

    public Object giftActiveCountUser(GiftActiveCountUserVo giftActiveCountUserV){
        Page page= PageHelper.startPage(giftActiveCountUserV.getPage(),giftActiveCountUserV.getLimit());
        List<UserGoodsProductDto> userGoodsProductDtos = agedmallFullGiftMapper.selectUserGoodsListByActivityId(
                giftActiveCountUserV.getId(), giftActiveCountUserV.getPhone(), giftActiveCountUserV.getCommodity(),
                giftActiveCountUserV.getOrderCode(), giftActiveCountUserV.getShopid()
        );
        Map<String, Object> map=new HashMap<>();
        map.put("total",page.getTotal());
        map.put("data",userGoodsProductDtos);
        return ResponseUtil.ok(map);
    }

}
